import 'dart:collection';
import 'dart:convert';
import 'package:cloud_phone_manager/Callback/Callback.dart';
import 'package:cloud_phone_manager/data/Data.dart';
import 'package:cloud_phone_manager/utils/CmdUtils.dart';
import 'package:cloud_phone_manager/utils/ConnectUtils.dart';
import 'package:flutter/cupertino.dart';
import 'package:http/http.dart' as http;

class NetManager {

  /// 请求设备信息
  static Future<NetResult> RequestDevicesInfo(String countryCode,
      String androidSdkVersion, String model, String gaid,
      String gaidType) async {
    final url = Uri.parse(
        'http://apicc-service.zhizh.com/api/v1/cloud/deviceModel/get');
    final HashMap<String, String> data = HashMap();
    if (countryCode.isNotEmpty) {
      data.putIfAbsent("countryCode", () => countryCode);
    }
    if (model.isNotEmpty) {
      data.putIfAbsent("model", () => model);
    }
    if (androidSdkVersion.isNotEmpty) {
      data.putIfAbsent("androidSdkVersion", () => androidSdkVersion);
    }
    if (gaid.isNotEmpty) {
      data.putIfAbsent("gaid", () => gaid);
    }
    if (gaidType == null || gaidType.isEmpty) {
      data.putIfAbsent("gaidType", () => "0");
    } else {
      data.putIfAbsent("gaidType", () => gaidType);
    }
    data.putIfAbsent("size", () => "1");
    NetResult netResult = NetResult(false, "");
    try {
      final response = await http.post(
        url,
        body: jsonEncode(data),
        headers: {
          'Content-Type': 'application/json; charset=UTF-8',
        },
        encoding: Encoding.getByName('utf-8'),
      );
      if (response.statusCode == 200) {
        netResult.isSuccess = true;
        netResult.result = utf8.decode(response.bodyBytes);
      } else {
        netResult.isSuccess = false;
        netResult.result = utf8.decode(response.bodyBytes);
      }
    } catch (error) {
      print('Error: $error');
    }
    return netResult;
  }

  static Future<NetResult> RequestChangeBaseInfo(String jsonStr) async {
    NetResult netResult = NetResult(false, "");
    Map<String, dynamic> jsonData = json.decode(jsonStr);
    Map<String, dynamic> modelData = jsonData["data"];
    List<dynamic> modelList = modelData["cloudDeviceModel"];
    if(modelList.isEmpty){
      return netResult;
    }
    String modelJSON = (modelList[0] as Map<String,dynamic>)["modelJson"];
    Map<String, dynamic> modelJSONData = json.decode(modelJSON);
    modelJSONData.putIfAbsent("sn", () => ConnectUtils().getCurrentChooseDevices()?.sn??"");
    debugPrint("读取的基础参数结果为：${modelJSONData}");
    final url = Uri.parse('http://apicc.motayun.net/api/v1/ruichi/modifyCard');
    /*final response = await http.post(url, body: jsonEncode(modelJSONData), headers: {
      'Content-Type': 'application/json; charset=UTF-8',
    }, encoding: Encoding.getByName('utf-8'));
    if (response.statusCode == 200) {
      netResult.isSuccess = true;
      netResult.result = utf8.decode(response.bodyBytes);
    } else {
      netResult.isSuccess = false;
      netResult.result = utf8.decode(response.bodyBytes);
    }*/
    return netResult;
  }

  static Future<NetResult> RequestChangeProp(String jsonStr) async {
    NetResult netResult = NetResult(false, "");
    Map<String, dynamic> jsonData = json.decode(jsonStr);
    Map<String, dynamic> modelData = jsonData["data"];
    List<dynamic> modelList = modelData["cloudDeviceModel"];
    if(modelList.isEmpty){
      return netResult;
    }
    String modelJSON = (modelList[0] as Map<String,dynamic>)["allPropJson"];
    Map<String, dynamic> modelJSONData = json.decode(modelJSON);
    Map<String,dynamic> upload = Map();
    upload.putIfAbsent("properties_list", () => modelJSONData);
    upload.putIfAbsent("sn", () => ConnectUtils().getCurrentChooseDevices()?.sn??"");
    debugPrint("读取的Prop参数结果为：${upload}");
    final url = Uri.parse('http://apicc.motayun.net/api/v1/ruichi/setSystemProperties');
    /*final response = await http.post(url, body: jsonEncode(modelJSON), headers: {
      'Content-Type': 'application/json; charset=UTF-8',
    }, encoding: Encoding.getByName('utf-8'));
    if (response.statusCode == 200) {
      netResult.isSuccess = true;
      netResult.result = utf8.decode(response.bodyBytes);
    } else {
      netResult.isSuccess = false;
      netResult.result = utf8.decode(response.bodyBytes);
    }*/
    return netResult;
  }
}